<?php

namespace backend\controllers;

use Yii;
use common\models\DayTry;
use backend\models\DayTrySearch;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use services\models\Product;

/**
 * DayTryController implements the CRUD actions for DayTry model.
 */
class DayTryController extends MyController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all DayTry models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new DayTrySearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        $product_info = Product::getGoodsInfo();
        $dataArray = [];
        if(!empty($product_info))
        {
            foreach($product_info as $v)
            {
                $dataArray[$v['product_id']] = ['goods_id'=>$v['goods_id'], 'goods_name'=>$v['goods_name']] ;
            }
        }
        
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'dataArray'=>$dataArray
        ]);
    }

    /**
     * Displays a single DayTry model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->renderPartial('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new DayTry model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new DayTry();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index', 'id' => $model->day_try_id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing DayTry model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index', 'id' => $model->day_try_id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing DayTry model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the DayTry model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return DayTry the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = DayTry::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
    
    public function actionGetGoodsInfo()
    {
        $product_id = Yii::$app->request->post('product_id');
        $json_array = ['data' => '', 'msg' => '货号不存在！' ,'flag' => 0];
        if (empty($product_id))
        {
            return json_encode($json_array);
        }
        $connection = Yii::$app->db;
        $sql = "SELECT * FROM d_try.`product` where product_id=$product_id";
        $command = $connection->createCommand($sql);
        $result = $command->queryOne();
        if(empty($result))
        {
            return json_encode($json_array);
        }
        $json_array = ['data' => $result, 'flag' => 1];
        return json_encode($json_array);
    }
    
    public function actionCheckTime()
    {
        $recommend_time = Yii::$app->request->post('recommend_time');
        $json_array = ['data' => '', 'msg' => '推荐时间已存在！' ,'flag' => 0];
        if (empty($recommend_time))
        {
            return json_encode($json_array);
        }
        $connection = Yii::$app->db;
        $sql = "SELECT * FROM d_try.`day_try` where recommend_time='$recommend_time'";
        $command = $connection->createCommand($sql);
        $result = $command->queryOne();
        if(!empty($result))
        {
            return json_encode($json_array);
        }
        $json_array = ['data' => $result, 'flag' => 1];
        return json_encode($json_array);
    }
    
    public function actionRefreshCache()
    {
        $datetime = date('Y-m-d 00:00:00');
        $connection = Yii::$app->db;
        
        $sql = "select * from d_try.day_try where status=1 order by recommend_time desc";     
        $result = $connection->createCommand($sql)->queryAll();
        if(!empty($result))
        {
            $flag = 0;
            $index_manage = array();
            foreach($result as $k=>$v)
            {
                if($v['recommend_time'] >= $datetime)
                {
                    $index_manage[$k]['product_id'] = $v['product_id'];
                    $index_manage[$k]['pic'] = $v['pic'];
                    $index_manage[$k]['recommend_time'] = $v['recommend_time'];
                }elseif($flag != 1)
                {
                    $index_manage[$k]['product_id'] = $v['product_id'];
                    $index_manage[$k]['pic'] = $v['pic'];
                    $index_manage[$k]['recommend_time'] = $v['recommend_time'];
                    $flag = 1;
                }
            }
            \Yii::$app->redis_index->set('day_try',  serialize($index_manage));
        }else
        {
            \Yii::$app->redis_index->set('day_try',  '');
        }
        $json_array = ['flag' => 1];
        return json_encode($json_array);
    }
    
    
    
}
